//
//  ComposeTextView.swift
//  swift_learning
//
//  Created by arons on 2017/2/3.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class ComposeTextView: UITextView , UITextViewDelegate{
    private lazy var placeHolderLabel = UILabel()
    
    /// 使用XIB加载自定义View的构造函数
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    private func setupUI() {
        // 1.添加子控件
        addSubview(placeHolderLabel)
        
        // 2.设置frame
        placeHolderLabel.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.left.equalTo(10)
        }
        
        // 3.设置placeholderLabel属性
        placeHolderLabel.textColor = UIColor.lightGray
        placeHolderLabel.font = font
        
        // 4.设置placeholderLabel文字
        placeHolderLabel.text = "分享新鲜事..."
        
        // 5.设置内容的内边距
        textContainerInset = UIEdgeInsets(top: 6, left: 7, bottom: 0, right: 7)
        
        // 设置内容改变监听
        // self.delegate = self;
        
        NotificationCenter.default.addObserver(self, selector: #selector(ComposeTextView.textDidChange), name: .UITextViewTextDidChange, object: nil)
    }
    
    deinit {
        BMLOG("==")
        // self.delegate = nil
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - ......::::::: UITextViewDelegate :::::::......
//    func textViewDidChange(_ textView: UITextView) {
//        if textView.text != nil && textView.text.characters.count > 0 {
//            placeHolderLabel.isHidden = true
//        }else {
//            placeHolderLabel.isHidden = false
//        }
//    }
    
    
    // MARK: - ......::::::: Notification :::::::......
    func textDidChange() {
        if self.text != nil && self.text.characters.count > 0 {
            placeHolderLabel.isHidden = true
        }else {
            placeHolderLabel.isHidden = false
        }
    }
    
}


